Macromedia Flex Article

Learning Flex Basics (Part 3) : Working with Containers


Table of Contents

  1. Introduction
  2. Building the Containers
  3. Create the Application Object
  4. Create a Data Array
  5. Add a TabNavigator Container for Global Navigation
  6. Add an HBox and Panel Containers for Main Views
  7. Add a ViewStack, LinkBar, and Panel Containers for Secondary Views
  8. Add a Form Container
  9. Add a Tile Layout Container and Populate It Using a Repeater
  10. Create a Pseudo Bar Chart Using a Repeater and VBoxes
  11. Complete Code Listing for containers.mxml
  12. Where to Go from Here

Create a Pseudo Bar Chart Using a Repeater and VBox Containers

In the final part of this tutorial, you deal with no new Flex classes, but instead put the VBox container to a different use: simulating a bar chart, to get a sense of the flexibility of containers.

You also learn how to change properties of repeated items at runtime. The key to this is knowing how to access the repeated items by id. All id values in an application must be unique, so if you set the id of a repeated item to a value like "myBox," myBox becomes an array whose elements are the id values of the items. You can then access the properties of the ith item through myBox[i].

To loop over all the repeated items, loop over the id array:

for (var i:Number=0;i<myBox.length;i++)
{
	[do something to mybox[i]...]
}
  1. Inside the Panel container with the id of "barPanel," add an HBox container with height bound to 80% of the surrounding Panel's height, and contents vertically aligned to the bottom:

    <mx:HBox height="{.8*barPanel.height}" verticalAlign="bottom">
    </mx:HBox>
    
  2. Inside the HBox container, add a Repeater object with the id "barRep" and the dataProvider bound to myArray:

    <mx:Repeater id="barRep" dataProvider="{myArray}">
    </mx:Repeater>
    
  3. Inside the Repeater object, add a VBox container with an id of vBar a backgroundColor of 0x99CCCC, height set to 20 times the currentItem of the Repeater, and contents horizontally aligned to the center:

    <mx:VBox id="vBar" backgroundColor="0x99CCCC" 
    	height="{20*barRep.currentItem}" width="40" horizontalAlign="center">
    </mx:VBox>
    
  4. Inside the VBox container, add a Label whose text property is bound the currentItem of the Repeater object:

    <mx:Label text="{barRep.currentItem}"/>
    
  5. After the HBox container, but still inside the Panel container, add a ControlBar container:

    <mx:ControlBar>
    </mx:ControlBar>
    
  6. Inside the ControlBar, add two Buttons, one with the label Scale Up and a click event that calls a function called scaleUp(), the other with the label Scale Down and a click event that calls scaleDown():

    <mx:Button label="Scale Up" click="scaleUp()"/>
    <mx:Button label="Scale Down" click="scaleDown()"/>
    
  7. In the Script block, add the scaleUp() function block, the function takes no parameters and returns nothing:

    function scaleUp():Void
    {
    	
    }
    
  8. Inside the function block, create a for loop that loops over the vBar array :

    for (var i:Number=0;i<vBar.length;i++)
    {
    }
    
  9. Inside the for loop, set the height of each vBar item to twice its current height:

    vBar[i].height=vBar[i].height*2;
    
  10. Inside the function block, create a for loop that loops over the vBar array :

    for (var i:Number=0;i<vBar.length;i++)
    {
    	vBar[i].height=vBar[i].height*2;
    }
    
  11. Add the scaleDown() function, which is identical to scaleUp() except that it sets each vBar height to half its current value:

    function scaleDown():Void
    {
    	for (var i:Number=0;i<vBar.length;i++)
    	{
    		vBar[i].height=vBar[i].height/2;
    	}
    }
    
  12. That's the end of the exercise. Save the file and browse it; try the buttons to see the bars in the graph scaled up and down.